package jp.co.ricoh.melody.models;

import java.util.ArrayList;
import java.util.List;

import jp.co.ricoh.melody.databases.DBManager;
import jp.co.ricoh.melody.models.entities.AlarmData;

/**
 * Copyright (C) 2016 RICOH Co.,LTD.
 * All rights reserved.
 * A implemented model for interacting with melody data.
 */
public class AlarmModel implements IAlarmModel, DBManager.onDataChangedListener {
    private static final String TAG = AlarmModel.class.getSimpleName();

    /**
     * Callbacks when melody data has been changed
     */
    public interface OnAlarmDataSetChangedListener {
        void onAlarmAdded(int alarmId);

        void onAlarmDeleted(int alarmId);

        void onAlarmChanged(int alarmId);
    }

    private static AlarmModel sAlarmModel = new AlarmModel();

    public static AlarmModel getInstance() {
        return sAlarmModel;
    }

    private DBManager mDatabaseManager = DBManager.getInstance();
    private List<OnAlarmDataSetChangedListener> mOnAlarmDataSetChangedListeners = new ArrayList<>();

    public AlarmModel() {
        mDatabaseManager.setOnDataChangedListener(this);
    }

    public void addOnAlarmDataSetChangedListener(OnAlarmDataSetChangedListener onAlarmDataSetChangedListener) {
        mOnAlarmDataSetChangedListeners.add(onAlarmDataSetChangedListener);
    }

    public void removeOnAlarmDataSetChangedListener(OnAlarmDataSetChangedListener onAlarmDataSetChangedListener) {
        mOnAlarmDataSetChangedListeners.remove(onAlarmDataSetChangedListener);
    }

    @Override
    public void onAlarmAdded(int alarmId) {
        for (OnAlarmDataSetChangedListener listener : mOnAlarmDataSetChangedListeners) {
            listener.onAlarmAdded(alarmId);
        }
    }

    public List<AlarmData> getAlarmList() {
        // can apply sort operation here

        return mDatabaseManager.getListAlarm();
    }

    @Override
    public boolean isFirstAddAlarm() {
        return mDatabaseManager.isFirstAdd();
    }

    @Override
    public void setFirstAddAlarm() {
        mDatabaseManager.setFirstAdd();
    }

    @Override
    public void onAlarmChanged(int alarmId) {
        for (OnAlarmDataSetChangedListener listener : mOnAlarmDataSetChangedListeners) {
            listener.onAlarmChanged(alarmId);
        }
    }

    @Override
    public void onAlarmRemoved(int alarmId) {
        for (OnAlarmDataSetChangedListener listener : mOnAlarmDataSetChangedListeners) {
            listener.onAlarmDeleted(alarmId);
        }
    }

    @Override
    public void deleteAlarm(AlarmData alarmData, DBManager.OnSaveDataResultListener onSaveDataResultListener) {
        mDatabaseManager.deleteAlarm(alarmData, onSaveDataResultListener);
    }

    @Override
    public AlarmData getAlarmById(int alarmId) {
        return mDatabaseManager.getAlarmById(alarmId);
    }

    @Override
    public void enableAlarm(boolean enabled, AlarmData alarmData, DBManager.OnSaveDataResultListener onSaveDataResultListener) {
        alarmData.setEnabled(enabled);
        mDatabaseManager.enableAlarm(enabled, alarmData, onSaveDataResultListener);
    }
}
